package com.woniuxy.ams.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.ams.entity.GroupRequest;
import com.woniuxy.ams.entity.PageRequest;
import com.woniuxy.ams.entity.ResultEntity;
import com.woniuxy.ams.entity.User;
import com.woniuxy.ams.service.UserService;
import com.woniuxy.ams.utils.SystemUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author fyx
 */
@RestController
@RequestMapping("/user")
public class UserController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping("/findAll")
    @RequiresRoles("Admin")
    public ResultEntity<?> findAllUserByPage(@RequestBody PageRequest<User> pageRequest) {
        PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getLimit());
        List<User> users = userService.findAllAndSearch(pageRequest.getData());
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return new ResultEntity<>(200, "查询成功", pageInfo);
    }

    @RequestMapping("/findAllByDept")
    public ResultEntity<?> findAllUserByDept(@RequestBody int id) {
        User user = new User();
        user.setDeptId(id);
        List<User> users = userService.findAllAndSearch(user);
        return new ResultEntity<>(200, "查询成功", users);
    }

    @PostMapping("/findUsersByGroup")
    public ResultEntity<?> findUsersByGroup(@RequestBody GroupRequest<User> groupRequest) {
        if (!groupRequest.isOnlySearch()) {
            PageHelper.startPage(groupRequest.getCurrentPage(), groupRequest.getLimit());
            List<User> users = userService.findUsersByGroup(groupRequest.getData(),groupRequest.getType());
            return new ResultEntity<>(200 ,"查询成功", new PageInfo<>(users));
        }
        List<User> users = userService.findUsersByGroup(groupRequest.getData(),groupRequest.getType());
        return new ResultEntity<>(200, "查询成功", users);
    }
    @RequestMapping("/findOne")
    public ResultEntity<?> findOneById(@RequestBody int id) {
        User user = userService.findOneById(id);
        if (user != null) {
            return new ResultEntity<>(200, "查询成功", user);
        }
        return new ResultEntity<>(404, "无此用户");
    }
    @DeleteMapping("/{name}")
    public ResultEntity<?> deleteUser(@PathVariable String name) {
        boolean isSuccess = userService.deleteUserByName(name);
        if (isSuccess) {
            return new ResultEntity<>(200, "删除成功");
        }
        return new ResultEntity<>(200, "删除失败");
    }

    @PostMapping("/delete")
    public ResultEntity<?> deleteUsers(@RequestBody List<Integer> ids) {
        boolean isSuccess = userService.deleteUserByIds(ids);
        if (isSuccess) {
            return new ResultEntity<>(200, "删除成功");
        }
        return new ResultEntity<>(200, "删除失败");
    }

    @PostMapping("/add")
    public ResultEntity<?> addUser(@RequestBody @Validated User user) {
        boolean isSuccess = userService.addUser(user);
        if (isSuccess) {
            return new ResultEntity<>(200, "添加成功");
        }
        return new ResultEntity<>(200, "添加失败");
    }

    @PostMapping("/resetPassword")
    public ResultEntity<?> resetPassword(@RequestBody User user) {
        boolean isSuccess = userService.resetPassword(user);
        if (isSuccess) {
            return new ResultEntity<>(200, "添加成功");
        }
        return new ResultEntity<>(200, "添加失败");
    }

    @PostMapping("/setPassword")
    public ResultEntity<?> setPassword(@RequestBody User user) {
        User loginUser = SystemUtils.getLoginUser();
        if (Objects.equals(loginUser.getName(), user.getName())) {
            return new ResultEntity<>(400, "未登录或无权修改别人的密码");
        }
        boolean isSuccess = userService.setPassword(user);
        if (isSuccess) {
            return new ResultEntity<>(200, "修改成功");
        }
        return new ResultEntity<>(200, "修改失败");
    }

    @PostMapping("/update")
    public ResultEntity<?> updateUser(@RequestBody User user) {
        boolean isSuccess = userService.updateByUser(user);
        if (isSuccess) {
            return new ResultEntity<>(200, "添加成功");
        }
        return new ResultEntity<>(200, "添加失败");
    }
}
